﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using HR.Ldap.Imp;
using Wicresoft.fjyc.Integration.HR.Interface;

namespace Temp.HRToLdapFromLocalSqlServer
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection("Data Source=.;Initial Catalog=fj_hr2;User ID=sa;Password=Password01!");
                conn.Open();
                
                /*--------------LDAPSync--------------*/
                //SqlCommand command = new SqlCommand();
                //command.Connection = conn;
                //command.CommandType = System.Data.CommandType.Text;
                //command.CommandText = "select PersonCode,DeptCode,StatusCode,uid from fj_HR2 where id > 19826";
                //reader = command.ExecuteReader();

                //LDAPSync s = new LDAPSync();
                //while (reader.Read())
                //{
                //    Employee emp = new Employee
                //    {
                //        PERSONCODE = reader["PersonCode"].ToString(),
                //        DEPTCODE = reader["DeptCode"].ToString(),
                //        O_UID = reader["uid"].ToString(),
                //        STATUSCODE = reader["StatusCode"].ToString()
                //    };
                //    s.PushEmployee(emp);
                //    Console.WriteLine(emp.O_UID + "has been import!");
                //}

                /*--------------UnifyShow--------------*/
//                SqlCommand command = new SqlCommand();
//                command.Connection = conn;
//                command.CommandType = System.Data.CommandType.Text;
//                command.CommandText = @"SELECT fh.CName,fo.OrgName,fh.Dept,fh.PositionName,fh.uid,fh.StatusCode
//FROM fj_HR2 fh
//INNER JOIN fj_orgmap map ON fh.DeptCode = map.srcorgcode
//INNER JOIN fj_Org2 fo ON map.finalorgcode = fo.OrgCode where fh.ID > 19826";
//                reader = command.ExecuteReader();
//                UnifyShowClient.UnifyShowClient us = new UnifyShowClient.UnifyShowClient();
//                while (reader.Read())
//                {

//                    Employee emp = new Employee
//                    {
//                        DEPT = reader["Dept"].ToString(),
//                        O_UID = reader["uid"].ToString(),
//                        STATUSCODE = reader["StatusCode"].ToString(),
//                        CNAME = reader["CName"].ToString(),
//                        COMPANY = reader["OrgName"].ToString(),
//                        POSITIONNAME = reader["PositionName"] == null ? null : reader["PositionName"].ToString()
//                    };
//                    try
//                    {
//                        us.PushEmployee(emp);
//                        Console.WriteLine(emp.O_UID + " has been import!");
//                    }
//                    catch (Exception ex)
//                    {
//                        Console.WriteLine(ex.Message);
//                        Console.Read();
//                        continue;
//                    }
//                }

                /*---------self service ---------*/

//                SqlCommand command = new SqlCommand();
//                command.Connection = conn;
//                command.CommandType = System.Data.CommandType.Text;
//                command.CommandText = @"SELECT fh.CName,fo.OrgName,fh.Dept,fh.PositionName,fh.uid,fh.StatusCode
//FROM fj_HR2 fh
//INNER JOIN fj_orgmap map ON fh.DeptCode = map.srcorgcode
//INNER JOIN fj_Org2 fo ON map.finalorgcode = fo.OrgCode where fh.ID > 19826";
//                reader = command.ExecuteReader();
//                SelfServiceClient.SelfServiceClient us = new SelfServiceClient.SelfServiceClient();
//                while (reader.Read())
//                {

//                    Employee emp = new Employee
//                    {
//                        DEPT = reader["Dept"].ToString(),
//                        O_UID = reader["uid"].ToString(),
//                        STATUSCODE = reader["StatusCode"].ToString(),
//                        CNAME = reader["CName"].ToString(),
//                        COMPANY = reader["OrgName"].ToString(),
//                        POSITIONNAME = reader["PositionName"] == null ? null : reader["PositionName"].ToString()
//                    };
//                    try
//                    {
//                        us.PushEmployee(emp);
//                        Console.WriteLine(emp.O_UID + " has been import!");
//                    }
//                    catch (Exception ex)
//                    {
//                        Console.WriteLine(ex.Message);
//                        Console.Read();
//                        continue;
//                    }
//                }


                /*---------ITSM ---------*/
                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = @"SELECT fh.CName,fo.OrgName,fh.Dept,fh.PositionName,fh.uid,fh.StatusCode,fh.PersonCode
FROM fj_HR2 fh
INNER JOIN fj_orgmap map ON fh.DeptCode = map.srcorgcode
INNER JOIN fj_Org2 fo ON map.finalorgcode = fo.OrgCode where fh.ID > 19826";
                reader = command.ExecuteReader();
                ITSMClient.ITSMClient us = new ITSMClient.ITSMClient();
                while (reader.Read())
                {

                    Employee emp = new Employee
                    {
                        DEPT = reader["Dept"].ToString(),
                        O_UID = reader["uid"].ToString(),
                        STATUSCODE = reader["StatusCode"].ToString(),
                        CNAME = reader["CName"].ToString(),
                        COMPANY = reader["OrgName"].ToString(),
                        POSITIONNAME = reader["PositionName"] == null ? null : reader["PositionName"].ToString(),
                        PERSONCODE = reader["PersonCode"].ToString()
                    };
                    try
                    {
                        us.PushEmployee(emp);
                        Console.WriteLine(emp.O_UID + " has been import!");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        //Console.Read();
                        continue;
                    }
                }
            }
            catch
            {

            }
            finally
            {                
                if(reader != null)
                    reader.Close();
                if(conn != null)
                    conn.Close();

            }



            Console.WriteLine("compelete.");
            Console.Read();
        }
    }
}
